import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:ditieapp/page/wiget/text_input.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import '../json/camare.dart';

class CamarePage extends StatefulWidget {
  @override
  _CamarePageState createState() => _CamarePageState();
}

class _CamarePageState extends State<CamarePage> {
  var base64 = "23423532";

  var base642 =
      "iVBORw0KGgoAAAANSUhEUgAAAEYAAAAjCAIAAACmdes6AAACP0lEQVR4Xu2W0U3EQAxETxSA+OMnFdAL30AV1EUproB2SGJwhpldZ7MJEBDSfCT22Ot3vj3dxV5f/pguGvrt+kf6Mg2P9xrs01mQbKY6BOxESK79YKdDcu0BOwype4JEfT1PjWRd6zo7kmsTWCvSzfXzKAo+PdyFxiPxVW2X26ui0KlZ9DSC7UJyIZJmyaaDqnLPKlgTkvPUkGweN7akWTd4Kpk1lHgCJqFaR3KSHMnmM3Ikf0jGDSUeJKmt63gkpcJgMm4o8SiDgq0gBcYmJKSi12TcRo9SeTDiGVIweEGO5DZC0qWNg+rnSgokdPqzCgv9tYpEaxmfa41CHsddEY9VxtU+dBZFsFBTGRK9YqTW3T4jUU+TLxV1QE/xiCjRqqVcQyY8HtGgS89uQUJ5bTGoo+txfISGiqMnSCE8bKj8psfEtfLw1Ob+biSDFRXPTpCiyTBvTGvJU0yZIsXcVNaIhJvBS4V4OZLBr2Jt7iQ1leOLz03C7hHURvaBhBG9VKvjuie31eLv5fiiPAme9kqQIk5fvGIrvUtkS2aYyjVUlC4H8RymeAZRrd6l8OjcRFg8bip3hyZIioSi26LZQ5BctfhSHj7NoRKkGDeaIN7w+b/ffiTsr6mpPEyaQ21CIiFeICVUhERtMYjZeFjuUm0glyLhZ+/SDrofxUM/AqMNq9zpDxhf/NgxCvq0tZxGqWW10LMa9PhPIpEIrw/J9Ec8sbZoZ3mI8FS1uJ0Wyda25AYNmiIl1hbtqSV1I70B+dAU/4dAw64AAAAASUVORK5C";

  var base643 =
      "gEOgS8BLwVPAS8FLwUvgU8BTwEugS4BDg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6A7YDuAQ4BDgDugO8BDoEOAQ2BDgEuAQ4BDwEOgQ4A7gDuAOyA7YDuAO4A7oEPAS+BLgEMgCuBLwEOAO8A7oDtgM4AzYDOgO6A7oDugO6BDwEOAO4BDgEOAQ6BDoENgQ0AzgDtgM4BDwEOAO2A7gDuAO4A7gENAO2A7QDNAO8BDoDtgO6A7gDtAO4BDwEOAO0A7YEOgQ4BDYBKAQ4A7YDugO2AzQDNgM2AzgDNgM0AzQDuAO4A7YDNAO2BDgEPAS4BDYDsgM2A7YEOgU8BLoEOAQ6BDYENgO4BDgDugO2A7IDugQ4BDoEOgQ4A7gEOgQ4BDYDsAO+AyYENgS4AaQDtgO0A7YDtAO2A7gDtgO6BDYDtAM0A7QDOgQ4A7QDtAO2BDoEOAQyAzIDOAQ6BLwFOgS4BDoEOAQ4BLoEvAS+BL4EugQ2BDoEOAQ2A7gDtgO2BDQENAO0A7IDsAMyA7IDvgIkBDYDtgQ2BDYENgO0A7YDuAQ4A7QDNAM0A7oEPAS4BDQDtAO2A7QDtAO0A7oEOAQ4BDYDuAQ6BLoEugS8BT4FPgU+BTwFOgSw==";

  var url1 = "https://188l877k45.zicp.vip";

  var url3 = "http://175.178.187.21:18000";

  var url; //本地环境 https://188l877k45.zicp.vip  //生产环境 http://175.178.187.21:18000

  List<Camare?> message = [];

  final TextEditingController _usernameController = TextEditingController();

  var descriptionCount = "";

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    setState(() {
      url = url3;
    });
    //getMessage();
  }

  void getTextInput() {
    //监听内容变化
    _usernameController.addListener(() {
      print("内容监听1：${_usernameController.text}");
    });

    //设置默认值
    _usernameController.text = "helloworld";
    //设置选择文本
    _usernameController.selection = TextSelection(
      baseOffset: 0,
      extentOffset: _usernameController.text.length,
    );
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _usernameController.dispose();
  }

  //获取连接信息
  void getMessage() async {
    var result = await new Dio().post(url3 + "/iot/camera/list");
    var time = result.data[0]["createTime"];
    DateTime nettime = DateTime.parse(time);
    //print(formatDate(nettime,[nettime.year,'年',nettime.month,'月',nettime.day]));
    //print(formatDate(DateTime.parse(time), [DateTime.parse(time).year.toString(),'年',DateTime.parse(time).month.toString(),'月',DateTime.parse(time).day.toString()+"日",' ',DateTime.parse(time).hour.toString(),':',DateTime.parse(time).minute.toString()]));

    // print(formatDate(_nowDate,[yyyy,'年',mm,'月',dd,' ',HH,':',nn]))
    List<Camare?> list = (result.data as List<dynamic>)
        .map((e) => Camare.fromMap((e as Map<String, dynamic>)))
        .toList();

    setState(() {
      message = list;
    });
  }

  //获取base64
  void getbase64() async {
    var result = await new Dio().post(url3 + "/iot/camera/list");
    var base64222 = result.data.toString();
    setState(() {
      base64 = base64222;
    });
    print(base64222);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("图传调试"),
        centerTitle: true,
      ),
      body: Stack(
        alignment: Alignment.center,
        children: [
          // Positioned(
          //   top: 45,
          //   child: Image.memory(
          //     base64Decode(base64),
          //     //防止重绘
          //     gaplessPlayback: true,
          //     width: 300,
          //     height: 300,
          //     fit: BoxFit.cover,
          //     //centerSlice: Rect.fromLTWH(0, 0, 0, 0),
          //     //alignment: AlignmentGeometry(1,3,4),
          //     //repeat: ImageRepeat.repeatY,
          //     //centerSlice: Rect.fromLTRB(20, 15, 40, 20),
          //   ),
          // ),
          Positioned(
            top: 45,
            child: Directionality(
              textDirection: TextDirection.rtl,
              child: Image.memory(
                base64Decode(base64),
                //防止重绘
                gaplessPlayback: true,
                width: 300,
                height: 300,
                fit: BoxFit.cover,
                matchTextDirection: true,
              ),
            ),
          ),
          Container(
            width: MediaQuery.of(context).size.width,
            height: MediaQuery.of(context).size.height,
            child: ListView(
              children: message
                  .map((e) => Padding(
                        padding: EdgeInsets.all(10),
                        child: Container(
                          width: MediaQuery.of(context).size.width,
                          height: 20,
                          child: Text(e!.message.toString()),
                        ),
                      ))
                  .toList(),
            ),
          ),
          Positioned(
              bottom: 200,
              child: InkWell(
                onTap: () {
                  //getMessage();
                  getbase64();
                },
                child: Container(
                    decoration: BoxDecoration(
                      color: Colors.blue,
                      borderRadius: BorderRadius.circular(10),
                    ),
                    width: 100,
                    height: 50,
                    child: Center(
                      child: Text(
                        "获取数据",
                        style:
                            TextStyle(fontSize: 18, color: Colors.amberAccent),
                      ),
                    )),
              )),
          // Positioned(
          //     bottom: 10,
          //     child: InkWell(
          //       onTap: () {
          //         //getMessage();
          //         setState(() {
          //           base64 = descriptionCount;
          //         });
          //       },
          //       child: Container(
          //           decoration: BoxDecoration(
          //             color: Colors.blue,
          //             borderRadius: BorderRadius.circular(10),
          //           ),
          //           width: 130,
          //           height: 50,
          //           child: Center(
          //             child: Text(
          //               "验证base64",
          //               style:
          //                   TextStyle(fontSize: 18, color: Colors.amberAccent),
          //             ),
          //           )),
          //     )),
          // Positioned(
          //   bottom: 70,
          //   child: Container(
          //     color: Colors.grey,
          //     width: MediaQuery.of(context).size.width,
          //     height: 100,
          //     child: TextField(
          //       autofocus: true,
          //       //maxLines: 3,
          //       //maxLength: 32,
          //       controller: _usernameController,
          //       onChanged: (value) {
          //         setState(() {
          //           descriptionCount = value;
          //         });
          //       },
          //       decoration: InputDecoration(
          //         labelText: "base64",
          //         hintText: "请粘贴base64",
          //         prefixIcon: const Icon(Icons.description),
          //         //counterText: "${descriptionCount.length}/32",
          //       ),
          //       keyboardType: TextInputType.multiline,
          //     ),
          //   ),
          // ),
        ],
      ),
    );
  }
}
